var gulp = require('gulp')
  , clean = require('gulp-clean')
  , uglify = require("gulp-uglify")
  , javascriptObfuscator = require('gulp-javascript-obfuscator')
  // https://www.npmjs.com/package/gulp-clean-css
  , cleanCSS = require('gulp-clean-css')
  , htmlmin = require('gulp-htmlmin')
  , imagemin = require('gulp-imagemin')
  , pngquant = require('imagemin-pngquant') // png图片压缩
  , pump = require('pump')
;

const options = {
  compact: true
  , sourceMap: false
  , debugProtection: true
  // , log: true
};

// https://obfuscator.io
// https://www.npmjs.com/package/javascript-obfuscator
const allOptions = {
  compact: true,
  controlFlowFlattening: false,
  controlFlowFlatteningThreshold: 0.75,
  deadCodeInjection: false,
  deadCodeInjectionThreshold: 0.4,
  debugProtection: false,
  debugProtectionInterval: false,
  disableConsoleOutput: false,
  domainLock: [],
  identifierNamesGenerator: 'hexadecimal', // mangled
  identifiersPrefix: '',
  inputFileName: '',
  log: false,
  renameGlobals: false,
  reservedNames: [],
  reservedStrings: [],
  rotateStringArray: true,
  seed: 0,
  selfDefending: false,
  sourceMap: false,
  sourceMapBaseUrl: '',
  sourceMapFileName: '',
  sourceMapMode: 'separate',
  stringArray: true,
  stringArrayEncoding: false,
  stringArrayThreshold: 0.75,
  target: 'browser',
  transformObjectKeys: false,
  unicodeEscapeSequence: false
};

// https://www.npmjs.com/package/gulp-uglify
gulp.task('uglify-js', () => {
  return gulp.src('src/js/*.js', {base: 'src'}) // 要压缩的js文件
    .pipe(uglify())  //使用uglify进行压缩,更多配置请参考：
    .pipe(gulp.dest('salary')); //压缩后的路径
});


// https://www.npmjs.com/package/gulp-javascript-obfuscator
gulp.task('minify-js', () => {
  return gulp.src('src/js/**/*.js', {base: 'src'})
    .pipe(javascriptObfuscator(options))
    .pipe(gulp.dest('salary'));
});


// https://www.npmjs.com/package/gulp-clean-css
gulp.task('minify-css', () => {
  return gulp.src('src/css/**/*.css', {base: 'src'})
    .pipe(cleanCSS({
      compatibility: 'ie8'
      , debug: true
    }, details => {
      console.log(`${details.name}: ${details.stats.originalSize}`);
      console.log(`${details.name}: ${details.stats.minifiedSize}`);
    }))
    .pipe(gulp.dest('salary'));
});

// https://www.npmjs.com/package/gulp-htmlmin
gulp.task('minify-html', () => {
  return gulp.src('src/**/*.html', {base: 'src'})
    .pipe(htmlmin({
      collapseWhitespace: true
    }))
    .pipe(gulp.dest('salary'));
});

gulp.task('minify-img', () => {
  return gulp.src('src/img/**/*.png', {base: 'src'})
    .pipe(imagemin({
      progressive: true,
      use: [pngquant()] //使用pngquant来压缩png图片
    }))
    .pipe(gulp.dest('salary'));
});

gulp.task('cp-fonts', () => {
  return gulp.src('src/fonts/**/*', {base: 'src'})
    .pipe(gulp.dest('salary'));
});
gulp.task('cp-img', () => {
  return gulp.src('src/img/**/*', {base: 'src'})
    .pipe(gulp.dest('salary'));
});

// https://www.npmjs.com/package/gulp-clean
gulp.task('clean', function (cb) {
  pump([
    gulp.src('salary', {allowEmpty: true}),
    clean()
  ], cb)
});

gulp.task('default', gulp.series('clean', 'minify-css', 'minify-js', 'minify-html', 'cp-fonts', 'cp-img'), () => {
  console.log('编译完成');
});


// base 作用： 保持目录结构
// gulp.src('client/js/**/*.js') // 匹配 'client/js/somedir/somefile.js' 并且将 `base` 解析为 `client/js/`
//   .pipe(minify())
//   .pipe(gulp.dest('build'));  // 写入 'build/somedir/somefile.js'
//
// gulp.src('client/js/**/*.js', {base: 'client'})
//   .pipe(minify())
//   .pipe(gulp.dest('build'));  // 写入 'build/js/somedir/somefile.js'
